package com.ck.GUI;

/**
 * Created by ck on 2016/12/19.
 */
        import com.blacamdk.ChatNetModule.ClientMain;
        import java.awt.*;
        import java.awt.event.*;
        import java.io.IOException;
        import java.net.URL;
        import javax.imageio.ImageIO;
        import javax.swing.*;
        import javax.swing.border.*;


/** 登录窗体 */
public class LoginFrame extends JFrame implements Runnable {
    private static final long serialVersionUID = -3426717670093483287L;

    private JTextField idTxt;
    private JPasswordField pwdFld;
    public static String username;
    public static ClientMain clientMain;

    public LoginFrame(){
        clientMain = new ClientMain();
    }

    public void init(){

//        Image imageIcon =Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("/Chat.png"));// 这种写法就可以解决导出的Jar包也能显示图标了
//        setIconImage(imageIcon);
        //初次测试成功但是导出后不显示图标
        //ImageIcon test = new ImageIcon(new String());//第一句
        //this.setIconImage(test.getImage()); //第二句
        //setIconImage(Toolkit.getDefaultToolkit().getImage("/images/chatblue.png") );
      //  URL url2 = LoginFrame.class.getResource("/Chat.png");
        String imagePath = "/Chat.png";// 构造图片的路径
        try{
            Image img= ImageIO.read(this.getClass().getResource(imagePath));
                this.setIconImage(img);
            } catch (IOException e) {
                e.printStackTrace();
            }
        this.setTitle("聊天室登录");
        this.setSize(330, 230);

        //设置默认窗体在屏幕中央
        int x = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        int y = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        this.setLocation((x - this.getWidth()) / 2, (y-this.getHeight())/ 2);
        this.setResizable(false);

        //把Logo放置到JFrame的北边
        URL url = LoginFrame.class.getResource("/logo.png");
        ImageIcon icon = new ImageIcon(url);
//        JLabel label = new JLabel();
//        label.setIcon(new ImageIcon("images/logo.png"));
//        this.add(label,BorderLayout.NORTH);
//        Icon icon = new ImageIcon("image/logo.png");
        JLabel label = new JLabel(icon);
        this.add(label, BorderLayout.NORTH);

        //登录信息面板
        JPanel mainPanel = new JPanel();
        Border border = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
        mainPanel.setBorder(BorderFactory.createTitledBorder(border, "输入登录信息", TitledBorder.CENTER,TitledBorder.TOP));
        this.add(mainPanel, BorderLayout.CENTER);
        mainPanel.setLayout(null);

        JLabel nameLbl = new JLabel("账号:");
        nameLbl.setBounds(50, 30, 40, 22);
        mainPanel.add(nameLbl);
        idTxt = new JTextField();
        idTxt.setBounds(95, 30, 150, 22);
        idTxt.requestFocusInWindow();//用户名获得焦点
        mainPanel.add(idTxt);

        JLabel pwdLbl = new JLabel("密码:");
        pwdLbl.setBounds(50, 60, 40, 22);
        mainPanel.add(pwdLbl);
        pwdFld = new JPasswordField();
        pwdFld.setBounds(95, 60, 150, 22);
        mainPanel.add(pwdFld);

        //按钮面板放置在JFrame的南边
        JPanel btnPanel = new JPanel();
        this.add(btnPanel, BorderLayout.SOUTH);
        btnPanel.setLayout(new BorderLayout());
        btnPanel.setBorder(new EmptyBorder(2, 8, 4, 8));

        JButton registeBtn = new JButton("注册");
        btnPanel.add(registeBtn, BorderLayout.WEST);
        JButton submitBtn = new JButton("登录");
        btnPanel.add(submitBtn, BorderLayout.EAST);

        //关闭窗口
        this.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e) {

                    System.exit(0);
                }
        });

        //注册
        registeBtn.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                new RegisterFrame();  //打开注册窗体
            }
        });

        //"登录"
        submitBtn.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                if(ClientMain.UID != -1L)
                    return;
                if (idTxt.getText().length() == 0
                        || pwdFld.getPassword().length == 0) {
                    JOptionPane.showMessageDialog(LoginFrame.this,
                            "账号和密码是必填的",
                            "输入有误",JOptionPane.ERROR_MESSAGE);
                    idTxt.requestFocusInWindow();
                    return;
                }
                String ret = ClientMain.login(idTxt.getText(), new String(pwdFld.getPassword()));
                if(ret.equals("成功")) {
                    username = idTxt.getText();
                    setVisible(false);// 本窗口隐藏
                }
                else
                {
                    //弹出提示框
                    JOptionPane.showMessageDialog(LoginFrame.this, ret);
                }
            }
        });
    }

    @Override
    public void run() {
        init();
        setVisible(true);
        while (ClientMain.UID == -1L)
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        dispose();
    }
}